<!DOCTYPE html
	PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>百度地图API-批量地址解译</title>
    <link href="css/Baidu.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div id="head">
        城市名称：<input id="city" />
        <input type="button" onclick="setCity()" id="setcity" value="定位"></input>
        | 经度：
        <input id="long"></input>
        纬度：<input id="lat"></input>
        <input type="button" onclick="setLatLonPosition()" id="setllpo" value="定位"></input>
        <input type="button" onclick="getPOI()" id="getpoi" value="获取POI"></input>
        <input type="text" id="info" style="width: 300px;"></input>
        <br /> 打开Excel：
        <input type="file" id="open-excel"></diid=>
        <input type="button" onclick="getallPOI()" id="bdgeo" value="获取所有POI"></input>
        <input type="button" onclick="setAllPosition()" id="addmarker" value="定位"></input>
        导出Excel： 选择路径:

        <input type="button" onclick="javascript:exportExcel('我的文件')" id="addmarker" value="导出Excel"></input>
        <!-- <input id="Button1" type="button" value="导出EXCEL" onclick="javascript:table2excel('test','我的文件')" /> -->
    </div>
    <div id="l-map"></div>
    <div class="open" id="open">
    </div>
</body>

</html>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="js/import.js"></script>
<script type="text/javascript" src="js/export.js"></script>
<script type="text/javascript" src="js/mymap.js"></script>
<!-- <script type="text/javascript" src="js/xlsx.core.min.js"></script> -->
<script src="https://cdn.bootcss.com/xlsx/0.11.9/xlsx.core.min.js"></script>
<!-- https://cdn.bootcss.com/xlsx/0.12.7/xlsx.js -->
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4&ak=PPDuSsbu2kdqP0o7WzvXQn1dB0NNzu4l"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?type=webgl&v=1.0&ak=PPDuSsbu2kdqP0o7WzvXQn1dB0NNzu4l"></script>
<script type="text/javascript">
    initMap();

    var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    var values; // 存储获取到的数据
    var index = 0;
    //坐标转换完之后的回调函数
    //读取表格文件内容
    $('#open-excel').change(function(e) {
        values = [];
        var files = e.target.files;
        var fileReader = new FileReader();
        fileReader.readAsBinaryString(files[0]); // 以二进制方式打开文件
        fileReader.onload = function(ev) {
            try {
                var data = ev.target.result,
                    workbook = XLSX.read(data, {
                        type: 'binary'
                    }),
                    value
                    // values = []; 
            } catch (e) {
                console.log('文件类型不正确');
                return;
            }

            // 遍历每张表读取
            for (var sheet in workbook.Sheets) {
                if (workbook.Sheets.hasOwnProperty(sheet)) {
                    // 得到表数据并转成json格式
                    let xlsxData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {
                        defval: '',
                        raw: false,
                        cellDates: true
                    });
                    values = values.concat(xlsxData);

                    json2Html(values);
                    //处理坐标，百度的偏移问题
                    // getallPoint();
                    // values = values.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                    break; // 如果只取第一张表，加上这行
                }
            }
        }

    });

    //获取POI的迭代函数
    function getallPOI() {
        // var mydata = values
        // var yindex = mydata.length;;
        getPOI3(values, index);
        index++;
    }
    //获取表格中大量数据的POI，并渲染到页面
    function getPOI3(mydata, index) {
        var indexObj = {};
        var description = '';
        // if (mydata[index] != undefined) {
        //     for (var i = 0; i <= 10; i++) {
        //         // var x = gg_lon;
        //         // var y = gg_lat;
        // var x = xdata[i];
        // var y = ydata[i];
        var x = mydata[index].lon;
        var y = mydata[index].lat;
        var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        mylon = z * Math.cos(theta) + 0.0065;
        mylat = z * Math.sin(theta) + 0.006;

        var lon = parseFloat(mylon.toFixed(6));
        var lat = parseFloat(mylat.toFixed(6));
        // console.log(bd_lon + ";" + bd_lat);
        // }
        // var lon = mydata[index].lon;
        // var lat = mydata[index].lat;
        var pt = new BMap.Point(lon, lat);
        if (pt) {
            var key = "" + pt.lng + pt.lat;
            indexObj[key] = index;
        }
        var geocoder = new BMap.Geocoder();
        var locationOptions = {
            poiRadius: 1500,
            numPois: 2
        }
        description = geocoder.getLocation(pt, function(result) {
                if (result) {
                    var cKey = '' + result.point.lng + result.point.lat;
                    var cIndex = indexObj[cKey]; //当前的index

                    description = result.address;
                    if (result.surroundingPois[0]) {
                        var poi = result.surroundingPois[0];
                        description += "，" +
                            poi.title + getDirDescription2(result.point, poi.point) + " " +
                            Math.round(map.getDistance(result.point, poi.point)) + "米，"
                    }
                    if (result.surroundingPois[1]) {
                        var poi = result.surroundingPois[1];
                        description += "" +
                            poi.title + getDirDescription2(result.point, poi.point) + " " +
                            Math.round(map.getDistance(result.point, poi.point)) + "米。"
                    }
                    mydata[cIndex]["address"] = description;

                    if (index == mydata.length - 1) {
                        json2Html(mydata);
                        values = mydata;
                        index = 0;
                        alert("地址加载处理完成");
                    }
                }
            },
            locationOptions);
        if (index < mydata.length - 1) {
            setTimeout(window.getallPOI, 20);
        }
    }
    //对表格中出现的所有经纬度进行定位
    function setAllPosition() { //参数：经纬度
        mydata = values;
        map.clearOverlays();
        for (var i = 0; i < mydata.length; i++) {

            var lon = mydata[i].lon;
            var lat = mydata[i].lat;

            var point = new BMap.Point(lon, lat);
            if (i == mydata.length - 1) {
                map.centerAndZoom(point, 15);
            }
            var marker = new BMap.Marker(point);
            // var info = "经度：" + lon + ";纬度：" + lat;
            // marker.setLabel(info);
            map.addOverlay(marker);

            var info = "经度：" + values[i].lon + ";纬度：" + values[i].lat;
            var label = new BMap.Label(info, {
                offset: new BMap.Size(10, -10)
            });
            marker.setLabel(label); //添加百度label
        }

    }

    //使用城市名进行定位
    function setCity() { //参数：城市名
        var cityname = document.getElementById("city").value;
        map.centerAndZoom(cityname, 15);
    }

    //使用经纬度进行定位
    function setLatLonPosition() { //参数：经纬度
        var x = document.getElementById("long").value;
        var y = document.getElementById("lat").value;
        var convertor = new BMapGL.Convertor();
        // var point = '';
        if (x != '' && y != '') {
            var ggPoint = new BMapGL.Point(x, y);
            var pointArr = [];
            pointArr.push(ggPoint);
            //纠偏并定位
            convertor.translate(pointArr, COORDINATES_WGS84, COORDINATES_BD09, function(data) {
                if (data.status === 0) {

                    var lon = parseFloat(data.points[0].lng.toFixed(6));
                    var lat = parseFloat(data.points[0].lat.toFixed(6));
                    var point = new BMap.Point(lon, lat);

                    map.centerAndZoom(point, 15);
                    map.clearOverlays();
                    // map.addOverlay(new BMap.Marker(point));

                    var marker = new BMap.Marker(point);
                    map.addOverlay(marker);
                    var info = "经度：" + x + ";纬度：" + y;
                    var label = new BMap.Label(info, {
                        offset: new BMap.Size(10, -10)
                    });
                    marker.setLabel(label); //添加百度label
                }
            })
        }
    }
    //单个点的获取POI
    function getPOI() {
        var x = document.getElementById("long").value;
        var y = document.getElementById("lat").value;
        var convertor = new BMapGL.Convertor();
        // var point = '';
        if (x != '' && y != '') {
            var ggPoint = new BMapGL.Point(x, y);
            var pointArr = [];
            pointArr.push(ggPoint);
            // 纠偏
            convertor.translate(pointArr, COORDINATES_WGS84, COORDINATES_BD09, function(data) {
                if (data.status === 0) {
                    var lon = parseFloat(data.points[0].lng.toFixed(6));
                    var lat = parseFloat(data.points[0].lat.toFixed(6));
                    var pt = new BMap.Point(lon, lat);
                    var geocoder = new BMap.Geocoder();
                    var locationOptions = {
                        poiRadius: 1500,
                        numPois: 2
                    }

                    var description = '';
                    // 根据经纬度获取POI
                    var description = geocoder.getLocation(pt, function(result) {
                        if (result) {
                            description = result.address;
                            if (result.surroundingPois[0]) {
                                var poi = result.surroundingPois[0];
                                description += "，" +
                                    poi.title + getDirDescription2(result.point, poi.point) + " " +
                                    Math.round(map.getDistance(result.point, poi.point)) + "米，"
                            }
                            if (result.surroundingPois[1]) {
                                var poi = result.surroundingPois[1];
                                description += "" +
                                    poi.title + getDirDescription2(result.point, poi.point) + " " +
                                    Math.round(map.getDistance(result.point, poi.point)) + "米。"
                            }

                            var info = document.getElementById("info");
                            info.setAttribute("value", description);
                        }
                    }, locationOptions);

                }
            })
        }



    }
</script>
</script>